﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Model
{
    public class BaseDataService
    {
        /// <summary>
        /// create
        /// </summary>
        /// <param name="summary"></param>
        /// <returns></returns>
        public ServiceResult<BaseDataSummary> SaveSummary(BaseDataSummary summary)
        {
            using (JzSportDbContext dbContext = new JzSportDbContext())
            {
                BaseDataRepository rep = new BaseDataRepository(dbContext);

                rep.CreateSummary(summary);

                if (dbContext.SaveChanges() > 0)
                {
                    return new ServiceResult<BaseDataSummary>(ErrorCode.NoError, summary);
                }
                return new ServiceResult<BaseDataSummary>(ErrorCode.DB_Error);
            }
        }

        /// <summary>
        /// update
        /// </summary>
        /// <param name="summary"></param>
        /// <returns></returns>
        public ServiceResult<BaseDataSummary> UpdateSammary(BaseDataSummary summary)
        {
            using (JzSportDbContext dbContext = new JzSportDbContext())
            {
                BaseDataRepository rep = new BaseDataRepository(dbContext);

                rep.UpdateSummary(summary);

                if (dbContext.SaveChanges() > 0)
                {
                    return new ServiceResult<BaseDataSummary>(ErrorCode.NoError, summary);
                }
                return new ServiceResult<BaseDataSummary>(ErrorCode.DB_Error);
            }
        }

        public ServiceResult<BaseDataItem> SaveItem(BaseDataItem item)
        {
            using (JzSportDbContext dbContext = new JzSportDbContext())
            {
                BaseDataRepository rep = new BaseDataRepository(dbContext);

                rep.CreateItem(item);

                if (dbContext.SaveChanges() > 0)
                {
                    return new ServiceResult<BaseDataItem>(ErrorCode.NoError, item);
                }
                return new ServiceResult<BaseDataItem>(ErrorCode.DB_Error);
            }
        }

        public ServiceResult<List<BaseDataSummary>> GetLatestSummaryList()
        {
            using (JzSportDbContext dbContext = new JzSportDbContext())
            {
                BaseDataRepository rep = new BaseDataRepository(dbContext);

                List<BaseDataSummary> lst = rep.GetLatestSummaryList();

                return new ServiceResult<List<BaseDataSummary>>(ErrorCode.NoError, lst);
            }
        }

        public ServiceResult<List<BaseDataItem>> GetItemList(Guid summaryId)
        {
            using (JzSportDbContext dbContext = new JzSportDbContext())
            {
                BaseDataRepository rep = new BaseDataRepository(dbContext);

                List<BaseDataItem> lst = rep.GetItems(summaryId);

                return new ServiceResult<List<BaseDataItem>>(ErrorCode.NoError, lst);
            }
        }

        public ServiceResult<BaseDataSummary> GetLatestSummary(string summaryCode)
        {
            using (JzSportDbContext dbContext = new JzSportDbContext())
            {
                BaseDataRepository rep = new BaseDataRepository(dbContext);

                return new ServiceResult<BaseDataSummary>(ErrorCode.NoError, rep.GetLatestSummary(summaryCode));
            }
        }
    }
}
